<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ParseUri
 *
 * @author wlf
 */
if(!defined('TinyFW')) exit('Access denied!');

class tfUri {
    
    private $app_name = null;
    private $module_name = null;
    private $action_name = null;

    private static $instance;

    
    public function __clone() {}
    
    private function __construct() {
        self::_parse_uri();
    }


    public static function singleton() {
        if(! isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    //分析uri,取得app,module,action的名字和其它參數
    private function _parse_uri() {
        $uri = $_SERVER['REQUEST_URI'];
        //echo "uri = ".$uri."<br>";
        $url = parse_url(WEB_ROOT);
        //print_r($url);

        //如果WEB_ROOT中的路徑存在於uri中，則將該path從uri中去除，以得到項目最頂的目錄
        if(strpos($uri,$url["PATH"]) == 0) {
            $uri = substr($uri,strlen($url["PATH"]));
        }

        //將uri以/分隔，以取得app_name, module_name and action name
        $auri = split("/",trim($uri,"/"));
        //print_r($auri);

        if(empty ($auri[0])) {
            $this->app_name = "default";
        } elseif (file_exists(APP_PATH . $auri[0]) && is_dir(APP_PATH . $auri[0])) {
            $this->app_name = $auri[0];
        } else {
            $this->app_name = "default";
        }
        //$this->app_name = (empty($auri[0])) ? 'default' : $auri[0];
        $this->module_name = (empty($auri[1])) ? "main" : $auri[1];
        $this->action_name = (empty($auri[2])) ?  "indexAction" : $auri[2]."Action";

        //處理get過來的數據
        for($i= 3; $i<count($auri); $i+=2) {
            //self::$request['get'][$auri[$i]] = $auri[$i+1];
            tfRequest::setGetParameter($auri[$i], $auri[$i+1]);
            
        }
        
        //處理post過來的數據
        tfRequest::setPostParameter($_POST);

        
        //Request::printParameters();
        //echo "<br>";
        //echo $this->app_name."<br>";
        //echo $this->module_name."<br>";
        //echo $this->action_name."<br>";
        //print_r($this->request);
    }

    private function _get_appname() {
        return $this->app_name;
    }

    private function _get_modulename() {
        return $this->module_name;
    }

    private function _get_actionname() {
        return $this->action_name;
    }
    public static function getAppName() {
        //return $this->app_name;
        return self::singleton()->_get_appname();
    }

    public static function getModuleName() {
        //return $this->module_name;
        return self::singleton()->_get_modulename();
    }

    public static function getActionName() {
        //return $this->action_name;
        return self::singleton()->_get_actionname();
    }
    


}
?>
